package face.pay.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

   public static final String default_pattern = "yyyy-MM-dd HH:mm:ss";
    /**
     * 获取精确到秒的时间戳
     * @return
     */
    public static String getSecondTimestamp(Date date){
        if (null == date) {
            return "0";
        }
        String timestamp = String.valueOf(date.getTime());
        int length = timestamp.length();
        if (length > 3) {
            return timestamp.substring(0,length-3);
        } else {
            return "0";
        }
    }

    public static Long getTime( Date date){

        return date.getTime();
    }

    public static String dateFormat(Date date){

        SimpleDateFormat sd =  new SimpleDateFormat(default_pattern);

        return sd.format(date);

    }
     public static String dateFormat(Date date,String pattern){

        SimpleDateFormat sd =  new SimpleDateFormat(pattern);

        return sd.format(date);

     }

    /**
     * 获取一天最晚的时间
     * @param date
     * @return
     */
    public static Date getDayLastTime(Date date){

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE,59);
        calendar.set(Calendar.SECOND,59);

        return  calendar.getTime();

    }

    public static Date dateParse(String date,String pattern){

        SimpleDateFormat sd =  new SimpleDateFormat(pattern);
        try {
            Date dateTime =   sd.parse(date);
            return  dateTime;

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }

    public static Date dateParse(String date){

        SimpleDateFormat sd =  new SimpleDateFormat(default_pattern);
        try {
            Date dateTime =   sd.parse(date);
            return  dateTime;

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;

    }





    public static String getNowTime(){
      return    dateFormat(new Date(),"yyyy-MM-dd HH:mm:ss");
    }


    public static void main(String[] args) {

        String second=  getSecondTimestamp(new Date());



        System.out.println(second);
    }



    public static Date addDay(Date time, Integer num) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.DAY_OF_MONTH, num);
        Date newTime = calendar.getTime();
        return newTime;
    }


    public static Date addMinute(Date time, Integer num) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.MINUTE, num);
        Date newTime = calendar.getTime();
        return newTime;
    }


    public static Date addSecond(Date time, Integer num) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.SECOND, num);
        Date newTime = calendar.getTime();
        return newTime;
    }



}
